﻿using System;
using System.Web.UI;
using System.Web.SessionState;

namespace Infrastructure.NetExtensions
{
    public static class StateExtensions
    {
        //
        // ViewState

        public static T Get<T>(this StateBag viewState, string key) 
        {
            return (viewState[key] != null) ? (T)viewState[key] : default(T);
        }

        public static void Set<T>(this StateBag viewState, string key, T value)
        {
            if (viewState[key] == null) viewState.Add(key, value);
            else viewState[key] = value;
        }

        //
        // Session

        public static T Get<T>(this HttpSessionState session, string key)
        {
            return (session[key] != null) ? (T)session[key] : default(T);
        }

        public static void Set<T>(this HttpSessionState session, string key, T value)
        {
            if (session[key] == null) session.Add(key, value);
            else session[key] = value;
        }
    }
}
